import axios from 'axios'

//axios 参数说明 ：   https://www.kancloud.cn/yunye/axios/234845
const instance = axios.create({
    timeout: 30000 // request timeout
});
//Vue.$request = instance;
//Vue.prototype.$request = instance

let getUrl = function(url) {
        //ENV_CONFIG: '"dev"',
        if (process.env.ENV_CONFIG == 'dev') {
            ////devApi
            ///用代理
            return '/devApi' + url

        } else {
            return process.env.BASE_API + url;
        }

    }
    // 请求完成后的行为，其中，requestCnt 和 计时器是用于避免重复密集调用loading产生的问题
    // 请求拦截器，发送前执行
instance.interceptors.request.use(
    config => {
        config.method = config.method || 'POST';
        if (config.method == 'post') {
            //config.headers['Content-Type'] ='application/x-www-form-urlencoded' 
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
                // 'application/json' //
        }
        config.url = getUrl(config.url);
        console.log(JSON.stringify(config));
        // alert(JSON.stringify(config));
        return config; // 返回请求配置信息
    },

    error => {
        // 请求发送失败          
        console.log(error) // for debug
        return Promise.reject(error) // 执行失败回调，参数为error
    }
)
instance.interceptors.response.use(
    response => {
        // alert(JSON.stringify(response));
        // console.log(response);
        response = response.data;
        if (response.code == 200 || response.code == 0) {
            return response;
        } else {
            return Promise.reject(response || response);
        }
    },
    error => {
        console.log(error);
        return Promise.reject(error.response || error);
    }
);
export default instance;